/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Httl1UITester.java
 *
 * Created on 2010-4-21, 10:12:06
 */
package net.labthink.instrument.device.httl1;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import net.labthink.instrument.device.httl1.codec.HttL1ProtocolCodecFactory;
import net.labthink.instrument.device.httl1.handler.Httl1handler;
import net.labthink.instrument.device.httl1.simulator.Httl1testDataProducer;
import net.labthink.instrument.rs232.RS232Connector;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.serial.SerialAddress;
import org.apache.mina.transport.serial.SerialAddress.DataBits;
import org.apache.mina.transport.serial.SerialAddress.FlowControl;
import org.apache.mina.transport.serial.SerialAddress.Parity;
import org.apache.mina.transport.serial.SerialAddress.StopBits;

/**
 *
 * @author Moses
 */
public class Httl1UITester extends javax.swing.JFrame {

    int testtype = 1;

    /** Creates new form Httl1UITester */
    public Httl1UITester() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup_testtype = new javax.swing.ButtonGroup();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        jRadioButton_testtype_renian = new javax.swing.JRadioButton();
        jRadioButton_testtype_boli = new javax.swing.JRadioButton();
        jRadioButton_testtype_kangla = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel_ComPort = new javax.swing.JLabel();
        jComboBox_ComPort = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Htt-L1辅助测试器");

        jButton1.setText("开始");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("参数设置"));
        jPanel1.setName(""); // NOI18N

        buttonGroup_testtype.add(jRadioButton_testtype_renian);
        jRadioButton_testtype_renian.setText("热粘试验");
        jRadioButton_testtype_renian.setToolTipText("热粘试验");
        jRadioButton_testtype_renian.setActionCommand("renian");
        jRadioButton_testtype_renian.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButton_testtype_renianItemStateChanged(evt);
            }
        });

        buttonGroup_testtype.add(jRadioButton_testtype_boli);
        jRadioButton_testtype_boli.setText("剥离试验");
        jRadioButton_testtype_boli.setToolTipText("剥离试验");
        jRadioButton_testtype_boli.setActionCommand("boli");
        jRadioButton_testtype_boli.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButton_testtype_boliItemStateChanged(evt);
            }
        });

        buttonGroup_testtype.add(jRadioButton_testtype_kangla);
        jRadioButton_testtype_kangla.setSelected(true);
        jRadioButton_testtype_kangla.setText("抗拉试验");
        jRadioButton_testtype_kangla.setToolTipText("抗拉试验");
        jRadioButton_testtype_kangla.setActionCommand("kangla");
        jRadioButton_testtype_kangla.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButton_testtype_kanglaItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton_testtype_renian)
                    .addComponent(jRadioButton_testtype_boli)
                    .addComponent(jRadioButton_testtype_kangla))
                .addContainerGap(45, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jRadioButton_testtype_kangla)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton_testtype_boli)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton_testtype_renian)
                .addContainerGap(96, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("串口设置"));

        jLabel_ComPort.setText("端口设置");

        jComboBox_ComPort.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8" }));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_ComPort)
                    .addComponent(jComboBox_ComPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(41, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel_ComPort)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox_ComPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(69, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(35, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //JOptionPane.showMessageDialog(this, testtype, "TestType Value", JOptionPane.INFORMATION_MESSAGE);
//        JOptionPane.showMessageDialog(this, jComboBox_ComPort.getSelectedItem(), "TestType Value", JOptionPane.INFORMATION_MESSAGE);
        SerialAddress portAddress = new SerialAddress((String) jComboBox_ComPort.getSelectedItem(), 9600, DataBits.DATABITS_8, StopBits.BITS_1, Parity.NONE,
                FlowControl.NONE);
        IoHandlerAdapter handler = new Httl1handler();
        RS232Connector receiver = RS232Connector.getInstance(handler, portAddress);

        receiver.addFilter("logger", new LoggingFilter());
        receiver.addFilter("codec", new ProtocolCodecFilter(new HttL1ProtocolCodecFactory()));    // 设置编码过滤器

        // receiver.addFilter("codec", new ProtocolCodecFilter(
        // new TextLineCodecFactory(Charset.forName("UTF-8")))); // 设置编码过滤器
        receiver.startListen();

        Httl1Tester01 test = new Httl1Tester01(receiver);
        Httl1testDataProducer.setKind(testtype);
        new Thread(test).start();
}//GEN-LAST:event_jButton1ActionPerformed

    private void jRadioButton_testtype_kanglaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButton_testtype_kanglaItemStateChanged
        testtype=1;
    }//GEN-LAST:event_jRadioButton_testtype_kanglaItemStateChanged

    private void jRadioButton_testtype_boliItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButton_testtype_boliItemStateChanged
        testtype=2;
    }//GEN-LAST:event_jRadioButton_testtype_boliItemStateChanged

    private void jRadioButton_testtype_renianItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButton_testtype_renianItemStateChanged
        testtype=3;
    }//GEN-LAST:event_jRadioButton_testtype_renianItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Httl1UITester.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Httl1UITester.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Httl1UITester.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Httl1UITester.class.getName()).log(Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Httl1UITester().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup_testtype;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox_ComPort;
    private javax.swing.JLabel jLabel_ComPort;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButton_testtype_boli;
    private javax.swing.JRadioButton jRadioButton_testtype_kangla;
    private javax.swing.JRadioButton jRadioButton_testtype_renian;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
}
